﻿#include "pch.h"

void FormatForGL3::apply(osg::Node& node)
{
	traverse(node);
}

void FormatForGL3::apply(osg::Geode& geode)
{
	traverse(geode);
}

void FormatForGL3::apply(osg::Geometry& geometry)
{
	// 法线:
	osg::Array *arrNormal = geometry.getNormalArray();
	if (arrNormal)
	{
		geometry.setVertexAttribArray(2, arrNormal);
	}

	// 帖图坐标:
	osg::Array *arrCoord = geometry.getTexCoordArray(0);
	if (arrCoord)
	{
		geometry.setVertexAttribArray(4, arrCoord);
	}

	osg::StateSet *stateSet1 = geometry.getOrCreateStateSet();

	if (stateSet1)
	{
		// 取材质球:
		osg::StateAttribute* abute = stateSet1->getAttribute(osg::StateAttribute::MATERIAL, 0);
		if (abute)
		{
			osg::ref_ptr<osg::Material> material = dynamic_cast<osg::Material*>(abute);
			if (material.valid())
			{

				osg::Vec4 diffuseColor = material->getDiffuse(osg::Material::FRONT);
				stateSet1->addUniform(new osg::Uniform("MaterialDiffuseColor", osg::Vec4f(diffuseColor)));
				if (1 > diffuseColor.w())
				{
					stateSet1->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
				}
			}
		}
	}










	traverse(geometry);
}